﻿using Newbe.BookmarkManager.Services.Common;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Text;

namespace Newbe.BookmarkManager.Services
{
    public static class WebHelper
    {
        private static readonly string[] TagColors =
        {
            "magenta", "pink", "red", "volcano", "orange", "green", "cyan", "blue", "lime", "geekblue", "purple"
        };

        public static DateTime? GetJwtExp(string token)
        {
            var jwt = new JwtSecurityTokenHandler();
            if (!jwt.CanReadToken(token))
            {
                throw new AccessTokenInvalidException(token);
            }
            var exp = jwt.ReadJwtToken(token).Claims.FirstOrDefault(a => a.Type == "exp")?.Value;
            if (string.IsNullOrWhiteSpace(exp))
            {
                throw new AccessTokenInvalidException(token);
            }
            return DateTimeOffset.FromUnixTimeSeconds(Convert.ToInt64(exp)).DateTime;
        }

        public static string GetTagColor(string tag)
        {
            var index = Encoding.UTF8.GetBytes(tag).Sum(x => x) % TagColors.Length;
            return TagColors[index];
        }

        public const string DefaultImagePlaceHolder =
            "";
    }
}